﻿package mortal.game.manager.msgTip
{
    import com.gengine.global.*;
    import com.gengine.utils.pools.*;
    import com.greensock.*;
    import flash.display.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.manager.*;

    public class MsgPlotShowImpl extends Sprite
    {
        private var _infoTxtFormat:TextFormat;
        private var _infoTxtFormatTW:TextFormat;
        private var _inTween:TweenMax;
        private var _outTween:TweenMax;
        private var _timerKey:uint;
        private var _lineWidth:int = 38;
        private var _inTimeOut:Number = 1;
        private var _outTimeOut:Number = 0.2;
        private var _outTimeOut2:Number = 0.5;
        private var _step:int;
        private var _msgList:Array;
        private var _endCallBack:Function;
        private var _lastClickTime:Number = 0;

        public function MsgPlotShowImpl()
        {
            this._infoTxtFormat = new GTextFormat(FontUtil.xingkaiName, 30, 16777215, false);
            this._infoTxtFormatTW = new GTextFormat(FontUtil.songtiName, 30, 16777215, false);
            this._msgList = [];
            this._infoTxtFormat.leading = 1;
            this._infoTxtFormat.align = TextFormatAlign.LEFT;
            this._infoTxtFormatTW.leading = 1;
            this._infoTxtFormatTW.align = TextFormatAlign.LEFT;
            mouseEnabled = false;
            mouseChildren = false;
            return;
        }// end function

        private function onMaskClickHandler() : void
        {
            var _loc_1:Number = NaN;
            if (this._lastClickTime != 0)
            {
                _loc_1 = getTimer();
                if (_loc_1 - this._lastClickTime > 400)
                {
                    this._lastClickTime = _loc_1;
                    return;
                }
                this._lastClickTime = _loc_1;
            }
            else
            {
                this._lastClickTime = getTimer();
                return;
            }
            switch(this._step)
            {
                case 1:
                {
                    this._inTimeOut = 0.1;
                    break;
                }
                case 2:
                {
                    clearTimeout(this._timerKey);
                    this.playHideEffect2();
                    break;
                }
                case 3:
                {
                    this._outTimeOut2 = 0.1;
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function showMsg(param1:String, param2:Function) : void
        {
            var _loc_3:Array = null;
            var _loc_6:String = null;
            var _loc_7:TextField = null;
            this.hideMsg();
            this._inTimeOut = 1;
            this._outTimeOut = 0.2;
            this._outTimeOut2 = 0.5;
            this.alpha = 1;
            this._endCallBack = param2;
            _loc_3 = param1.split("|");
            var _loc_4:int = 0;
            var _loc_5:* = _loc_3.length;
            var _loc_8:* = Global.stage.stageWidth / 2 + _loc_3.length * this._lineWidth;
            if (_loc_8 > Global.stage.stageWidth - 320)
            {
                _loc_8 = Global.stage.stageWidth - 320;
            }
            while (_loc_4 < _loc_5)
            {
                
                _loc_7 = ObjectPool.getObject(TextField);
                if (ParamsConst.instance.proxyType == ProxyType.TW)
                {
                    _loc_7.defaultTextFormat = this._infoTxtFormatTW;
                }
                else
                {
                    _loc_7.defaultTextFormat = this._infoTxtFormat;
                }
                _loc_7.width = this._lineWidth;
                _loc_7.autoSize = TextFieldAutoSize.LEFT;
                _loc_7.multiline = true;
                _loc_7.wordWrap = true;
                _loc_7.mouseEnabled = false;
                _loc_7.filters = [FilterConst.plotShowFilter2, FilterConst.plotShowFilter, FilterConst.sceneDesFilter];
                _loc_7.x = _loc_8 - _loc_4 * this._lineWidth;
                _loc_7.y = 0;
                _loc_7.text = _loc_3[_loc_4];
                this._msgList.push(_loc_7);
                _loc_4++;
            }
            LayerManager.highestLayer.addChild(this);
            this.stageReseze();
            this.playShowEffect();
            return;
        }// end function

        private function playShowEffect() : void
        {
            var _loc_1:TextField = null;
            if (this._msgList.length > 0)
            {
                _loc_1 = this._msgList.shift() as TextField;
                _loc_1.alpha = 0;
                this.addChild(_loc_1);
                this._inTween = TweenMax.to(_loc_1, this._inTimeOut, {alpha:1, onComplete:this.playShowEffect});
                this._step = 1;
            }
            else
            {
                this._timerKey = setTimeout(this.playHideEffect2, 3000);
                this._step = 2;
            }
            return;
        }// end function

        private function playHideEffect(param1:Boolean = false) : void
        {
            var _loc_2:TextField = null;
            this._step = 3;
            if (this.numChildren > 0)
            {
                _loc_2 = this.getChildAt(0) as TextField;
                if (!param1)
                {
                    _loc_2.text = "";
                    _loc_2.filters = null;
                    this.removeChild(_loc_2);
                    ObjectPool.disposeObject(_loc_2, TextField);
                    if (this.numChildren > 0)
                    {
                        _loc_2 = this.getChildAt(0) as TextField;
                    }
                }
                if (_loc_2.parent)
                {
                    this._outTween = TweenMax.to(_loc_2, this._outTimeOut, {alpha:0, onComplete:this.playHideEffect});
                }
                else
                {
                    this.hideEffectEnd();
                }
            }
            else
            {
                this.hideEffectEnd();
            }
            return;
        }// end function

        private function playHideEffect2() : void
        {
            this._step = 3;
            this._outTween = TweenMax.to(this, this._outTimeOut2, {alpha:0, onComplete:this.hideEffectEnd});
            return;
        }// end function

        private function hideEffectEnd() : void
        {
            this._step = 0;
            if (this._endCallBack != null)
            {
                this._endCallBack();
            }
            this.hideMsg();
            return;
        }// end function

        public function stageReseze() : void
        {
            if (parent != null)
            {
                this.y = (Global.stage.stageHeight - this.height) / 2;
                if (y > 70)
                {
                    y = 70;
                }
            }
            return;
        }// end function

        public function hideMsg() : void
        {
            var _loc_1:TextField = null;
            this._lastClickTime = 0;
            this._step = 0;
            this._endCallBack = null;
            EffectManager.hideUIMask();
            if (this.parent)
            {
                this.parent.removeChild(this);
            }
            if (this._inTween && this._inTween.active)
            {
                this._inTween.kill();
            }
            if (this._outTween && this._outTween.active)
            {
                this._outTween.kill();
            }
            clearTimeout(this._timerKey);
            while (this.numChildren > 0)
            {
                
                _loc_1 = this.removeChildAt(0) as TextField;
                _loc_1.filters = null;
                _loc_1.text = "";
                ObjectPool.disposeObject(_loc_1, TextField);
            }
            while (this._msgList.length > 0)
            {
                
                _loc_1 = this._msgList.shift() as TextField;
                _loc_1.filters = null;
                _loc_1.text = "";
                ObjectPool.disposeObject(_loc_1, TextField);
            }
            return;
        }// end function

        override public function get height() : Number
        {
            var _loc_3:int = 0;
            var _loc_4:TextField = null;
            var _loc_1:int = 1;
            var _loc_2:* = numChildren;
            while (_loc_1 < _loc_2)
            {
                
                _loc_4 = getChildAt(_loc_1) as TextField;
                if (_loc_4 && _loc_4.textHeight > _loc_3)
                {
                    _loc_3 = _loc_4.textHeight;
                }
                _loc_1++;
            }
            _loc_1 = 0;
            _loc_2 = this._msgList.length;
            while (_loc_1 < _loc_2)
            {
                
                _loc_4 = this._msgList[_loc_1] as TextField;
                if (_loc_4 && _loc_4.textHeight > _loc_3)
                {
                    _loc_3 = _loc_4.textHeight;
                }
                _loc_1++;
            }
            return _loc_3;
        }// end function

        override public function get width() : Number
        {
            return (numChildren + this._msgList.length) * this._lineWidth;
        }// end function

    }
}
